package com.cugbmao.lucene;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class WebHttpClient
{

	public static void main(final String[] args) throws IOException
	{
		try
		{
			// 生成下载对象
			Socket webclient = new Socket("www.bnu.edu.cn", 80);
			PrintWriter result = new PrintWriter(webclient.getOutputStream(),
					true);
			BufferedReader receiver = new BufferedReader(new InputStreamReader(
					webclient.getInputStream()));

			// 发送HTTP request请求
			result.println("GET / HTTP/1.1");
			result.println("Host: bnu.edu.cn");
			result.println("Connection: Close");
			result.println();

			// 接收HTTP Response 返回的结果信息
			boolean bRet = true;
			StringBuffer sb = new StringBuffer(8096);
			while (bRet)
			{
				if (receiver.ready())
				{
					int idx = 0;
					while (idx != -1)
					{
						idx = receiver.read();
						sb.append((char) idx);
					}
					bRet = false;
				}
			}

			// 显示获得的网页正文，打印到控制台
			System.out.println(sb.toString());
			webclient.close();
		}
		catch (UnknownHostException e)
		{
			System.err.println("无法访问指定主机.");
			System.exit(1);
		}
		catch (IOException e)
		{
			System.err.println("下载失败，请检查输入地址是否正确。");
			System.exit(1);
		}
	}

}
